fix(light/linear): mark pytree_token as ephemeral via __getstate__/__setstate__#374
Merged
Merged
Conversation
…setstate__ pytree_token is a process-local counter increment used as a stable hash/eq identity for LightProfileLinear instances across jax.jit flatten/unflatten. It is not meaningful state to persist — copying a number from one process's counter into another's does not preserve identity across processes. PyAutoFit's database serializer routes every numeric attribute through the Value SQL row (sa.Float column), so persisting pytree_token as an int and reading it back yields a float. Python >=3.12 strictly requires __hash__ to return int, so any dict keyed on a LightProfileLinear (e.g. the linear_light_profile_intensity_dict in autogalaxy/abstract_fit.py) raised TypeError on the visualization path after a fit was loaded via FitImagingAgg. Add __getstate__ that omits pytree_token and __setstate__ that assigns a fresh value if missing. PyAutoFit's Instance._from_object and Object.__call__ already check for these methods and honour them, so no PyAutoFit changes are required. The JAX-jit path uses register_model's attr_const which reads vars(self) directly and is unaffected. Fixes the smoke-test failure on autolens_workspace_test/main: scripts/database/scrape/general.py. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Mark
LightProfileLinear.pytree_tokenas runtime-only via Python's standard__getstate__/__setstate__protocol, so it doesn't get lossy-int-to-float persisted through PyAutoFit's database round-trip.Why
The CI
Smoke Testsjobs onautolens_workspace_test/main(both 3.12 and 3.13) have been failing with:Root cause:
pytree_token(added in29b16b47to give a stable identity acrossjax.jitflatten/unflatten) is set in__init__tonext(itertools.count())— anint. When a fit is loaded back viaFitImagingAgg, PyAutoFit's database deserializer routes every numeric attribute through theValueSQL row (sa.Floatcolumn ininstance.py:96), so the saved int comes back as a float (0.0/1.0observed in live trace). Python ≥3.12 strictly rejects non-intreturns from__hash__.What this PR does
pytree_tokenis a process-local counter increment — not meaningful state to persist (copying a number from one process's counter into another's preserves nothing). Adding__getstate__/__setstate__is the canonical Python idiom for marking an attribute as ephemeral.PyAutoFit's
Instance._from_object(atinstance.py:73) andObject.__call__(atmodel.py:189) already honour these methods. No PyAutoFit changes required. The JAX path usesregister_model'sattr_constwhich readsvars(self)directly and is unaffected by__getstate__.Why not patch PyAutoFit's database serializer
A library-wide fix in PyAutoFit (e.g. an
IntValueSQL table) would preserve every persisted int — butpytree_tokenis the only known consumer of int-typed persisted state whose__hash__breaks.Galaxy.__hash__already doesint(self.id),GeometryProfile.__hash__returnsid(self). The conceptually-correct fix lives at the layer where the attribute is defined: this attribute should never have been persisted.Tests added
5 new unit tests in
test_autogalaxy/profiles/light/linear/test_abstract.py:test__pytree_token_is_int_and_uniquetest__getstate__omits_pytree_tokentest__setstate__assigns_fresh_pytree_token_when_missingtest__pickle_roundtrip_preserves_int_hashtest__setstate__preserves_pytree_token_when_presentAll pure-numpy (no JAX imports), per the project's library-test policy.
Verified locally
pytest test_autogalaxy/→ 845 passed (was 840 + 5 new)python autolens_workspace_test/scripts/database/scrape/general.py→ exit 0, prints "FitImagingAgg Checked" with no tracebackOut of scope
AssertionErrorfailures inautolens_workspace_test/scripts/jax_likelihood_functions/...smoke tests — separate root cause, separate PR.Test plan
Testsworkflow goes green on this PRautolens_workspace_testSmoke Tests onmainand confirmTypeError: __hash__ method should return an integeris gone🤖 Generated with Claude Code